import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class StudentTool {
    static String[] student;

    /**
     * 对所有学生按平均分的高低进行排序 （需要确保对象数组已被赋值）
     * @param students
     * @throws FileNotFoundException
     */
    public static void sort(Student[] students) throws FileNotFoundException {
        System.out.println("输出排序后的数组：");
        int studentnumber = readLine("Students.txt");
        //用来储存结果
        String[][] result = new String[studentnumber][6];
        //step1.把信息填入数组
        for (int i = 0; i < result.length; i++) {
            result[i][0] = students[i].getName();
            result[i][1] = students[i].getMath() + "";
            result[i][2] = students[i].getJava() + "";
            result[i][3] = students[i].getDs() + "";
            result[i][4] = students[i].getAvg() + "";
            result[i][5] = students[i].getTotal() + "";
        }
        //step2:根据平均成绩进行排序 冒泡排序..有点low
        for (int i = 0; i < result.length-1; i++) {
            for (int j = 0; j < result.length-1; j++) {
                if (Integer.valueOf(result[j][4]).intValue() < Integer.valueOf(result[j+1][4]).intValue()) {
                    String[] temp = result[j];
                    result[j] = result[j+1];
                    result[j+1] = temp;
                }
            }
        }
        //step3:输出内容
        printArray(result);
    }

    /**
     * 把读取与制作成对象合成一个函数 并且返回的是对象（构造函数不能有返回值 只能再写一个函数了）
     *
     * @param fileName
     * @return
     * @throws FileNotFoundException
     */
    public static Student[] studentToolMain(String fileName) throws FileNotFoundException {
        Student[] result;
        student = readStudentsFromFile(fileName);
        result = makeStudentFromString(student);
        return result;
    }

    /**
     * 统计文件里的行数
     */
    private static int readLine(String filename) throws FileNotFoundException {
        Scanner scanner = new Scanner(new File(filename));
        int linenumber = 0;
        while (scanner.hasNextLine()) {
            scanner.nextLine();
            linenumber++;
        }
        scanner.close();
        return linenumber;
    }

    /**
     * 把文件信息读取到String数组
     *
     * @param fileName
     * @return String[]
     * @throws FileNotFoundException
     */
    public static String[] readStudentsFromFile(String fileName) throws FileNotFoundException {
        int i = 0;
        //统计一下文件中的行数
        int linenumber = readLine(fileName);
        //创建一个数组用来暂存学生信息 并且最终返回这个数组
        String[] student = new String[linenumber];
        Scanner scanner = new Scanner(new File(fileName));
        while (scanner.hasNextLine()) {
            //把每一行的信息都放入数组
            student[i] = scanner.nextLine();
            i++;
        }
        scanner.close();
        return student;
    }

    /**
     * 把数组中的数据转化为 对象
     *
     * @param students
     * @return Stusent[]
     */
    public static Student[] makeStudentFromString(String[] students) {
        //创建对象数组
        Student[] students1 = new Student[students.length];
        //对数组中的每一个元素进行扫描 并实现为一个对象
        for (int i = 0; i < students.length; i++) {
            String studentinfo = students[i];
            //创建扫描器
            Scanner scanner = new Scanner(studentinfo);
            //设置间隔符为空格
            scanner.useDelimiter(" ");
            //对设置的对象赋值
            students1[i] = new Student();
            students1[i].setName(scanner.next());
            students1[i].setMath(scanner.nextInt());
            students1[i].setJava(scanner.nextInt());
            students1[i].setDs(scanner.nextInt());
            students1[i].setAvg();
            students1[i].setTotal();
        }
        return students1;
    }


    /**
     * 输出二维数组的值
     */
    private static void printArray(String[][] array){
        for (int i = 0; i < array.length; i++) {
            for (int j = 0; j < array[i].length; j++) {
                System.out.print(array[i][j] + "   ");
            }
            System.out.println();
        }


    }
}
